<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="HatBoy" />



<meta name="description" content="编译和运行Cython代码">
<meta name="keywords" content="Python,Cython">
<meta property="og:type" content="article">
<meta property="og:title" content="第二章 编译和运行Cython代码">
<meta property="og:url" content="http://hatboy.gitee.io/2017/07/03/第二章-编译和运行Cython代码/index.html">
<meta property="og:site_name" content="HatBoy的个人主页">
<meta property="og:description" content="编译和运行Cython代码">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-12-21T09:58:59.111Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第二章 编译和运行Cython代码">
<meta name="twitter:description" content="编译和运行Cython代码">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="HatBoy的个人主页" type="application/atom+xml">



    <link rel="shortcut icon" href="/img/favicon.ico">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>第二章 编译和运行Cython代码 | HatBoy的个人主页</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>





    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?04d04bb57e5c5eaf49059aa602440263";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>


</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.jpg" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">HatBoy</a></h1>
        </hgroup>

        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="true" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:hatboy-dj@qq.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="https://github.com/HatBoy/" title="GitHub"></a>
                            
                                <a class="fa GitHub博客" href="https://hatboy.github.io/" title="GitHub博客"></a>
                            
                                <a class="fa CSDN" href="http://blog.csdn.net/dj1174232716" title="CSDN"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Cython/">Cython</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/">Python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python-tips/">Python tips</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python模块tips/">Python模块tips</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python语法/">Python语法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/asyncio/">asyncio</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/异步并发/">异步并发</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/调试/">调试</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="http://fmxfmx.gitee.io/">FMX</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://jewelz.me/">Jewelz</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://www.zhihu.com/people/talentisan/activities">轻狂书生</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://threetop.top/">ZCC</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://blog.csdn.net/u012017783">ZCC的CSDN</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://zcc888.gitee.io/">ZCC的博客</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">一枚爱Python,爱海贼,爱健身,喜欢好玩有趣事物的程序员……</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">HatBoy</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.jpg" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">HatBoy</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:hatboy-dj@qq.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="https://github.com/HatBoy/" title="GitHub"></a>
                            
                                <a class="fa GitHub博客" target="_blank" href="https://hatboy.github.io/" title="GitHub博客"></a>
                            
                                <a class="fa CSDN" target="_blank" href="http://blog.csdn.net/dj1174232716" title="CSDN"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-第二章-编译和运行Cython代码" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2017/07/03/第二章-编译和运行Cython代码/" class="article-date">
      <time datetime="2017-07-03T09:00:00.000Z" itemprop="datePublished">2017-07-03</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      第二章 编译和运行Cython代码
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        

        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Cython/">Cython</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Python/">Python</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <excerpt in="" index="" |="" 首页摘要="">

<p>编译和运行Cython代码</p>
<a id="more"></a>
<the rest="" of="" contents="" |="" 余下全文="">

<p>Cython在运行之前必须要编译，编译可以是隐式的或者是显式的</p>
<h2 id="本章介绍几种Cython的编译方式让他可以同Python一起运行："><a href="#本章介绍几种Cython的编译方式让他可以同Python一起运行：" class="headerlink" title="本章介绍几种Cython的编译方式让他可以同Python一起运行："></a>本章介绍几种Cython的编译方式让他可以同Python一起运行：</h2><ul>
<li>Cython代码能够被编译并且在IPython中交互运行</li>
<li>Cython代码可以在导入的时候自动编译</li>
<li>Cython可以通过构建工具单独的编译</li>
<li>Cython可以结合到标准的构建系统中，如make, CMake或者SCons</li>
</ul>
<h2 id="Cython编译管道（Pipeline）"><a href="#Cython编译管道（Pipeline）" class="headerlink" title="Cython编译管道（Pipeline）"></a>Cython编译管道（Pipeline）</h2><p>管道的左右是转换Cython代码到Python的扩展模块，让其能被Python的解释器导入和使用。管道编译有两个步骤，第一个步骤是通过cython编译器转换Cython源码成优化过的平台独立的C或者C++代码，第二个步骤是通过标准的C或者C++编译器将生成的C或者C++源码编译成共享库。编译后的共享库是平台相关性的，在Linux或者Mac OS X系统下是.so扩展名的共享库，在Windows系统下是.pyd扩展名的动态链接库。</p>
<h2 id="标准的方法：使用distutils和cythonize"><a href="#标准的方法：使用distutils和cythonize" class="headerlink" title="标准的方法：使用distutils和cythonize"></a>标准的方法：使用distutils和cythonize</h2><p>通过使用Python的distutils模块和Cython的cythonize命令，显示的进行编译，也是最常用的方法。</p>
<h3 id="例子：将fib-pyx源码编译成Linux系统下的fib-so共享库或者Windows系统下的fib-pyd文件"><a href="#例子：将fib-pyx源码编译成Linux系统下的fib-so共享库或者Windows系统下的fib-pyd文件" class="headerlink" title="例子：将fib.pyx源码编译成Linux系统下的fib.so共享库或者Windows系统下的fib.pyd文件"></a>例子：将fib.pyx源码编译成Linux系统下的fib.so共享库或者Windows系统下的fib.pyd文件</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#fib.pyx</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">fib</span><span class="params">(int n)</span>:</span></div><div class="line">    cdef int i</div><div class="line">    cdef double a=<span class="number">0.0</span>, b=<span class="number">1.0</span></div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(n):</div><div class="line">        a, b = a + b, a</div><div class="line">    <span class="keyword">return</span> a</div><div class="line"></div><div class="line"><span class="comment">## C函数库调用</span></div><div class="line">如何使用 Cython 调用 C 库函数？简单来说，我们先以一个 C 标准库中的函数为例。 你不需要向你的代码中引入 额外的依赖，Cython 都已经帮你定义好了这些函数。所以你可以将这些函数直接 cimport 进来并使用。</div><div class="line">举个例子，比如说当你想用最简单的方法将char*类型的值转化为一个整型值时， 你可以使用atoi() 函数，这个函数是在stdlib.h 头文件中定义的。我们可以这样来写：</div><div class="line">```python</div><div class="line"><span class="keyword">from</span> libc.stdlib cimport atoi</div><div class="line"></div><div class="line">cdef parse_charptr_to_py_int(char* s):</div><div class="line">    <span class="keyword">assert</span> s <span class="keyword">is</span> <span class="keyword">not</span> NULL, <span class="string">"byte string value is NULL"</span></div><div class="line">    <span class="keyword">return</span> atoi(s)   <span class="comment"># note: atoi() has no error detection!</span></div></pre></td></tr></table></figure>
<p>你可以在 Cython 的源代码包<a href="https://github.com/cython/cython/tree/master/Cython/Includes中找到所有的标准" target="_blank" rel="external">https://github.com/cython/cython/tree/master/Cython/Includes中找到所有的标准</a> cimport 文件。这些文件保存在.pxd 文件中，这是一种标准再模块间共享 Cython 函数声明的方法。<br>Cython 也有一整套的 Cython 的C-API 函数声明集。 例如，为了测试你的 Cython 代码的 C 编译时间，你可以这样做：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">from</span> cpython.version cimport PY_VERSION_HEX</div><div class="line"></div><div class="line"><span class="comment"># Python version &gt;= 3.2 final ?</span></div><div class="line"><span class="keyword">print</span> PY_VERSION_HEX &gt;= <span class="number">0x030200F0</span></div></pre></td></tr></table></figure></p>
<p>Cython 也提供了 C math 库的声明：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">from</span> libc.math cimport sin</div><div class="line"></div><div class="line">cdef double f(double x):</div><div class="line">    <span class="keyword">return</span> sin(x*x)</div></pre></td></tr></table></figure></p>
<p>#setup.py<br>from distutils.core import setup<br>from Cython.Build import cythonize</p>
<p>setup(ext_modules=cythonize(‘fib.pyx’))</p>
<p>#通过命令行编译<br>$ python setup.py build_ext –inplace<br>等价于<br>$ python setup.py build_ext -i</p>
<p>#Windows系统要指定额外的参数<br>python setup.py build_ext -i –compiler=mingw32 -DMS_WIN64<br>或者<br>python setup.py build_ext -i –compiler=msvc<br>根据编译器的不同填写参数</p>
<p>#直接可以在IPython中import fib使用该模块<br><figure class="highlight arduino"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line">### 例子：使用Cython包装C和C++代码</div><div class="line">```python</div><div class="line"><span class="comment">/*cfib.h*/</span></div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> __CFIB_H__</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> __CFIB_H__</span></div><div class="line"></div><div class="line"><span class="keyword">double</span> cfib(<span class="keyword">int</span> n);</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></div><div class="line"></div><div class="line"><span class="comment">/*cfib.c*/</span></div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"cfib.h"</span></span></div><div class="line"></div><div class="line"><span class="keyword">double</span> cfib(<span class="keyword">int</span> n) &#123;</div><div class="line">    <span class="keyword">int</span> i;</div><div class="line">    <span class="keyword">double</span> a=<span class="number">0.0</span>, b=<span class="number">1.0</span>, tmp;</div><div class="line">    <span class="built_in">for</span> (i=<span class="number">0</span>; i&lt;n; ++i) &#123;</div><div class="line">        tmp = a; a = a + b; b = tmp;</div><div class="line">    &#125;</div><div class="line">    <span class="built_in">return</span> a;</div><div class="line">&#125;</div><div class="line"></div><div class="line">#wrap_fib.pyx</div><div class="line"></div><div class="line">cdef <span class="keyword">extern</span> from <span class="string">"cfib.h"</span>:</div><div class="line">    <span class="keyword">double</span> cfib(<span class="keyword">int</span> n)</div><div class="line"></div><div class="line">def fib(n):</div><div class="line">    <span class="string">''</span><span class="string">' Returns the nth Fibonacci number.'</span><span class="string">''</span></div><div class="line">    <span class="built_in">return</span> cfib(n)</div><div class="line"></div><div class="line"><span class="meta">#setup.py</span></div><div class="line"></div><div class="line">from distutils.core <span class="keyword">import</span> <span class="built_in">setup</span>, Extension</div><div class="line">from Cython.Build <span class="keyword">import</span> cythonize</div><div class="line"></div><div class="line">exts = cythonize([Extension(<span class="string">"wrap_fib"</span>, sources=[<span class="string">"cfib.c"</span>, <span class="string">"wrap_fib.pyx"</span>])])</div><div class="line"></div><div class="line"><span class="built_in">setup</span>(ext_modules=cythonize(ext))</div><div class="line"></div><div class="line">#如果提供的是预编译后的动态链接库libfib.so而不是源码，则<span class="built_in">setup</span>.py如下</div><div class="line">from distutils.core <span class="keyword">import</span> <span class="built_in">setup</span>, Extension</div><div class="line">from Cython.Build <span class="keyword">import</span> cythonize</div><div class="line"></div><div class="line">ext = Extension( name=<span class="string">"wrap_fib"</span>, sources=[<span class="string">"wrap_fib.pyx"</span>], library_dirs=[<span class="string">"/path/to/libfib.so"</span>], libraries=[<span class="string">"fib"</span>] )</div><div class="line"></div><div class="line"><span class="built_in">setup</span>(ext_modules=cythonize(ext))</div></pre></td></tr></table></figure></p>
<h2 id="在IPython中和Cython进行交互"><a href="#在IPython中和Cython进行交互" class="headerlink" title="在IPython中和Cython进行交互"></a>在IPython中和Cython进行交互</h2><p>在IPython中可以通过魔法命令%load_ext Cython和%%cython与Cython代码进行交互<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">3</span>]: %load_ext Cython</div><div class="line"></div><div class="line">In [<span class="number">4</span>]: %%cython</div><div class="line">   ...: <span class="function"><span class="keyword">def</span> <span class="title">fib</span><span class="params">(int n)</span>:</span></div><div class="line">   ...:      cdef int i</div><div class="line">   ...:      cdef double a=<span class="number">0.0</span>, b=<span class="number">1.0</span></div><div class="line">   ...:      <span class="keyword">for</span> i <span class="keyword">in</span> range(n):</div><div class="line">   ...:          a, b = a+b, a</div><div class="line">   ...:      <span class="keyword">return</span> a</div><div class="line">   ...: </div><div class="line"></div><div class="line">In [<span class="number">5</span>]: fib(<span class="number">10</span>)</div><div class="line">Out[<span class="number">5</span>]: <span class="number">55.0</span></div></pre></td></tr></table></figure></p>
<p>除了%%cython命令外还有%%cython_inline和%%cython_pyximport两个命令可以使用。</p>
<h2 id="使用pyximport在导入时-进行编译"><a href="#使用pyximport在导入时-进行编译" class="headerlink" title="使用pyximport在导入时 进行编译"></a>使用pyximport在导入时 进行编译</h2><p>pyximport在import时识别.pyx扩展模块，然后自动将他们编译后导入<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">1</span>]: <span class="keyword">import</span> pyximport</div><div class="line"></div><div class="line">In [<span class="number">2</span>]: pyximport.install()</div><div class="line">Out[<span class="number">2</span>]: (<span class="keyword">None</span>, &lt;pyximport.pyximport.PyxImporter at <span class="number">0x7fc79e1ee588</span>&gt;)</div><div class="line"></div><div class="line">In [<span class="number">3</span>]: <span class="keyword">import</span> fib</div><div class="line"></div><div class="line">In [<span class="number">4</span>]: fib.__file__</div><div class="line">Out[<span class="number">4</span>]: <span class="string">'/home/dj/.pyxbld/lib.linux-x86_64-3.5/fib.cpython-35m-x86_64-linux-gnu.so'</span></div></pre></td></tr></table></figure></p>
<p>由于pyximport依赖cython编译器和C编译器，往往生产环境都不在控制中。</p>
<h2 id="管理pyximport的依赖"><a href="#管理pyximport的依赖" class="headerlink" title="管理pyximport的依赖"></a>管理pyximport的依赖</h2><ul>
<li>有时候Cython源文件依赖其他的源文件如C、C++源代码，头文件或者其他的Cython源代码，在这种情况下当依赖文件更新的时候pyximport必须重新编译导入.pyx文件，这个时候可以使用.pyxdeps扩展名的文件，来列出.pyx所依赖的所有文件，文件内容可以是通配符也可以是依赖文件的列表。如果.pyxdeps文件存在，pyximport在导入的时候会比较.pyx的的修改时间，将其重新编译导入。</li>
<li>怎样告诉pyximport编译和链接几个源文件到一个扩展模块，这个时候需要.pyxbld文件，其目的是为不同的情况定制pyximport</li>
<li>像.pyxdeps和.pyxbld文件都是基于相同文件名的Cython的.pyx源代码，用.pyxbld替换.pyx扩展，他们应该被放置在被导入的.pyx的文件的同一个目录</li>
</ul>
<p>.pyxbld文件中的是什么内容：一个或者两个函数</p>
<ul>
<li><p>make_ext(modname, pyxfilename)：如果定义了该函数，该函数接受两个参数，他返回一个distutils.extension.Extension实例，或者等效于调用Cython.Build.cythonize返回的结果，允许用户定制Extension，如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">make_ext</span><span class="params">(modname, pyxfilename)</span>:</span></div><div class="line">    <span class="keyword">from</span> distutils.extension <span class="keyword">import</span> Extension</div><div class="line">    <span class="keyword">return</span> Extension(modname,</div><div class="line">            sources=[pyxfilename, <span class="string">'_fib.c'</span>],</div><div class="line">            include_dirs = [<span class="string">'.'</span>])</div></pre></td></tr></table></figure>
</li>
<li><p>make_setup_args()：如果这个函数定义了，pyximport获取额外的参数传给distutils.core.setup，用于控制setup的编译过程，如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">make_setup_args</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> dict(script_args=[<span class="string">"--compiler=mingw32"</span>])</div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="完整的依赖扩展的例子"><a href="#完整的依赖扩展的例子" class="headerlink" title="完整的依赖扩展的例子"></a>完整的依赖扩展的例子</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div></pre></td><td class="code"><pre><div class="line">/*_fib.c*/</div><div class="line"></div><div class="line"><span class="comment">#include "_fib.h"</span></div><div class="line"></div><div class="line">unsigned long int fib(unsigned long int n)</div><div class="line">&#123;</div><div class="line">    unsigned long int a, b, tmp, i;</div><div class="line">    a = <span class="number">0</span>; b = <span class="number">1</span>;</div><div class="line">    <span class="keyword">for</span> (i=<span class="number">0</span>; i&lt;n; ++i) &#123;</div><div class="line">        tmp = a; a += b; b = tmp;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> a;</div><div class="line">&#125;</div><div class="line"></div><div class="line">/*_fib.h*/</div><div class="line"></div><div class="line"><span class="comment">#ifndef __FIB_H__</span></div><div class="line"><span class="comment">#define __FIB_H__</span></div><div class="line"></div><div class="line">unsigned long int fib(unsigned long int);</div><div class="line"></div><div class="line"><span class="comment">#endif</span></div><div class="line"></div><div class="line"><span class="comment">#fib.pyx</span></div><div class="line"></div><div class="line">cdef extern <span class="keyword">from</span> <span class="string">"_fib.h"</span>:</div><div class="line">    unsigned long int _cfib <span class="string">"fib"</span>(unsigned long int)</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">cfib</span><span class="params">(unsigned long int n)</span>:</span></div><div class="line">    <span class="keyword">return</span> _cfib(n)</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">fib</span><span class="params">(long n)</span>:</span></div><div class="line">    <span class="string">'''Returns the nth Fibonacci number.'''</span></div><div class="line">    cdef long a=<span class="number">0</span>, b=<span class="number">1</span>, i</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(n):</div><div class="line">        a, b = a + b, a</div><div class="line">    <span class="keyword">return</span> a</div><div class="line"></div><div class="line"><span class="comment">#fib.pyxbld</span></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">make_ext</span><span class="params">(modname, pyxfilename)</span>:</span></div><div class="line">    <span class="keyword">from</span> distutils.extension <span class="keyword">import</span> Extension</div><div class="line">    <span class="keyword">return</span> Extension(modname,</div><div class="line">            sources=[pyxfilename, <span class="string">'_fib.c'</span>],</div><div class="line">            include_dirs = [<span class="string">'.'</span>])</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">make_setup_args</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> dict(script_args=[<span class="string">"--compiler=mingw32"</span>])</div><div class="line"></div><div class="line"><span class="comment">#fib.pyxdeps</span></div><div class="line"></div><div class="line">_fib.*</div><div class="line"></div><div class="line"><span class="comment">#user_pyximport.py</span></div><div class="line"></div><div class="line"><span class="keyword">import</span> pyximport</div><div class="line">pyximport.install()</div><div class="line"></div><div class="line"><span class="keyword">import</span> fib</div><div class="line"></div><div class="line">print(<span class="string">"fib.__file__"</span>, fib.__file__)</div><div class="line">print(<span class="string">"fib.fib(90):"</span>, fib.fib(<span class="number">90</span>))</div><div class="line">print(<span class="string">"fib.cfib(90):"</span>, fib.cfib(<span class="number">90</span>))</div></pre></td></tr></table></figure>
<h2 id="自己动手手动编译"><a href="#自己动手手动编译" class="headerlink" title="自己动手手动编译"></a>自己动手手动编译</h2><p>我们想从fib.pyx源代码创建一个扩展模块，但是不想使用distutils、IPython或者是pyximport，我们该怎么办？<br>这里有两步可以做到：第一步从Cython源码生成C/C++源码，第二步编译C/C++源码为一个扩展模块。<br>第一步很简单，用cython命令，将Cython源码转换成C/C++源码：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ cython fib.pyx</div></pre></td></tr></table></figure></p>
<p>第二步使用GCC编译C/C++源码，全部过程如下：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$ CFLAGS=$(python-config --cflags)</div><div class="line">$ LDFLAGS=$(python-config --ldflags)</div><div class="line">$ cython fib.pyx <span class="comment"># --&gt; outputs fib.c</span></div><div class="line">$ gcc -c fib.c $&#123;CFLAGS&#125; <span class="comment"># outputs fib.o</span></div><div class="line">$ gcc fib.o -o fib.so -shared $&#123;LDFLAGS&#125; <span class="comment"># --&gt; outputs fib.so</span></div></pre></td></tr></table></figure></p>
<p>手动编译对平台环境要求较高，不同的平台环境参数不一样</p>
<h2 id="用其他构建系统使用Cython"><a href="#用其他构建系统使用Cython" class="headerlink" title="用其他构建系统使用Cython"></a>用其他构建系统使用Cython</h2><h3 id="CMake和Cython"><a href="#CMake和Cython" class="headerlink" title="CMake和Cython"></a>CMake和Cython</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># Detects and activates Cython</span></div><div class="line">include(UseCython)</div><div class="line"></div><div class="line"><span class="comment"># Specifies that Cython source files should generate C++</span></div><div class="line">set_source_files_properties($&#123;CYTHON_CMAKE_EXAMPLE_SOURCE_DIR&#125;/src/file.pyx   PROPERTIES   CYTHON_IS_CXX TRUE )</div><div class="line"></div><div class="line"><span class="comment"># Adds and compiles Cython source into an extension module</span></div><div class="line">cython_add_module( modname file.pyx   cpp_source.cxx)</div></pre></td></tr></table></figure>
<h3 id="SCons和Cython"><a href="#SCons和Cython" class="headerlink" title="SCons和Cython"></a>SCons和Cython</h3><h3 id="Make和Cython"><a href="#Make和Cython" class="headerlink" title="Make和Cython"></a>Make和Cython</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">INCDIR := $(shell python -c  <span class="string">"from distutils import sysconfig; print(sysconfig.get_python_inc())"</span>)</div><div class="line"></div><div class="line">LIBS := $(shell python -c   <span class="string">"from distutils import sysconfig;  print(sysconfig.get_config_var('LIBS'))"</span>)</div></pre></td></tr></table></figure>
<h2 id="将Python源码利用Cython编译成可执行二进制"><a href="#将Python源码利用Cython编译成可执行二进制" class="headerlink" title="将Python源码利用Cython编译成可执行二进制"></a>将Python源码利用Cython编译成可执行二进制</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># test.py</span></div><div class="line"><span class="keyword">from</span> math <span class="keyword">import</span> pi, e</div><div class="line"><span class="keyword">print</span> <span class="string">"e**pi == &#123;:.2f&#125;"</span>.format(e**pi)</div><div class="line"><span class="keyword">print</span> <span class="string">"pi**e == &#123;:.2f&#125;"</span>.format(pi**e)</div><div class="line"></div><div class="line"><span class="comment">#使用cython编译器将Python源码转换成C代码</span></div><div class="line">$ cython --embed irrationals.py</div><div class="line"></div><div class="line"><span class="comment">#将C代码编译成可执行二进制</span></div><div class="line">$ gcc $(python-config --cflags) $(python-config --ldflags) ./irrationals.c</div><div class="line"><span class="comment">#注意：该二进制执行还是依赖Python的动态链接库</span></div></pre></td></tr></table></figure>
<h2 id="编译指令"><a href="#编译指令" class="headerlink" title="编译指令"></a>编译指令</h2><p>Cython提供了编译指令来控制如何编译Cython源码，指令能指定四个不同的作用域，并且能很容易的打开或者关闭测试或者Debug选项，但是并不是所有的指令都能设置每一个域。<br>所有的指令都可以在Cython代码的第一行通过注释来添加，如下：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># cython: nonecheck=True</span></div><div class="line"><span class="comment"># cython: boundscheck=False</span></div><div class="line"></div><div class="line">也可以通过逗号分隔不同的指令：</div><div class="line"><span class="comment"># cython: nonecheck=True, boundscheck=False</span></div></pre></td></tr></table></figure></p>
<p>当然，我们也可以通过命令行参数-X或者–directive选项来设置编译指令，如下:<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ cython --directive nonecheck=<span class="keyword">False</span> source.pyx</div></pre></td></tr></table></figure></p>
<p>有些编译指令支持函数和上下文级别的作用域的控制，如装饰器和上下文管理器，实例如下：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">cimport cython</div><div class="line"></div><div class="line"><span class="meta">@cython.boundscheck(False)</span></div><div class="line"><span class="meta">@cython.wraparound(False)</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">fast_indexing</span><span class="params">()</span>:</span></div><div class="line">    <span class="comment"># ...</span></div><div class="line"></div><div class="line"></div><div class="line">cimport cython</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">fast_indexing</span><span class="params">(a)</span>:</span></div><div class="line">    <span class="keyword">with</span> cython.boundscheck(<span class="keyword">False</span>), cython.wraparound(<span class="keyword">False</span>):</div><div class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> range(len(a)):</div><div class="line">            sum += a[i]</div></pre></td></tr></table></figure></p>
</the></excerpt>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/2017/07/03/第二章-编译和运行Cython代码/">第二章 编译和运行Cython代码</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">HatBoy</a></p>
        <p><span>发布时间:</span>2017-07-03, 17:00:00</p>
        <p><span>最后更新:</span>2017-12-21, 17:58:59</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/2017/07/03/第二章-编译和运行Cython代码/" title="第二章 编译和运行Cython代码">http://hatboy.gitee.io/2017/07/03/第二章-编译和运行Cython代码/</a>
            <span class="copy-path" data-clipboard-text="原文: http://hatboy.gitee.io/2017/07/03/第二章-编译和运行Cython代码/　　作者: HatBoy" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/2017/07/17/第三章-深入Cython/">
                    第三章 深入Cython
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/2017/07/02/第一章-Cython简介/">
                    第一章 Cython简介
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#本章介绍几种Cython的编译方式让他可以同Python一起运行："><span class="toc-number">1.</span> <span class="toc-text">本章介绍几种Cython的编译方式让他可以同Python一起运行：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Cython编译管道（Pipeline）"><span class="toc-number">2.</span> <span class="toc-text">Cython编译管道（Pipeline）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#标准的方法：使用distutils和cythonize"><span class="toc-number">3.</span> <span class="toc-text">标准的方法：使用distutils和cythonize</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#例子：将fib-pyx源码编译成Linux系统下的fib-so共享库或者Windows系统下的fib-pyd文件"><span class="toc-number">3.1.</span> <span class="toc-text">例子：将fib.pyx源码编译成Linux系统下的fib.so共享库或者Windows系统下的fib.pyd文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#在IPython中和Cython进行交互"><span class="toc-number">4.</span> <span class="toc-text">在IPython中和Cython进行交互</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用pyximport在导入时-进行编译"><span class="toc-number">5.</span> <span class="toc-text">使用pyximport在导入时 进行编译</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#管理pyximport的依赖"><span class="toc-number">6.</span> <span class="toc-text">管理pyximport的依赖</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#完整的依赖扩展的例子"><span class="toc-number">6.1.</span> <span class="toc-text">完整的依赖扩展的例子</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#自己动手手动编译"><span class="toc-number">7.</span> <span class="toc-text">自己动手手动编译</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#用其他构建系统使用Cython"><span class="toc-number">8.</span> <span class="toc-text">用其他构建系统使用Cython</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CMake和Cython"><span class="toc-number">8.1.</span> <span class="toc-text">CMake和Cython</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SCons和Cython"><span class="toc-number">8.2.</span> <span class="toc-text">SCons和Cython</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Make和Cython"><span class="toc-number">8.3.</span> <span class="toc-text">Make和Cython</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#将Python源码利用Cython编译成可执行二进制"><span class="toc-number">9.</span> <span class="toc-text">将Python源码利用Cython编译成可执行二进制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#编译指令"><span class="toc-number">10.</span> <span class="toc-text">编译指令</span></a></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"第二章 编译和运行Cython代码　| HatBoy的个人主页　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    




    <div class="scroll" id="post-nav-button">
        
            <a href="/2017/07/17/第三章-深入Cython/" title="上一篇: 第三章 深入Cython">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/2017/07/02/第一章-Cython简介/" title="下一篇: 第一章 Cython简介">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2018/02/27/如何恢复丢失的Python源代码如果它还在内存中运行/">如何恢复丢失的Python源代码如果它还在内存中运行</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/02/08/pathlib：文件系统路径作为对象/">pathlib：文件系统路径作为对象</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio：异步IO、事件循环和并发/">asyncio：异步IO、事件循环和并发</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之调试asyncio/">asyncio之调试asyncio</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之协程结合线程和进程/">asyncio之协程结合线程和进程</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之接收Unix信号/">asyncio之接收Unix信号</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之使用子进程/">asyncio之使用子进程</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之与DNS服务进行交互/">asyncio之与DNS服务进行交互</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之使用SSL/">asyncio之使用SSL</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之使用协程和流的异步I-O/">asyncio之使用协程和流的异步I/O</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之协议抽象类的异步I-O/">asyncio之协议抽象类的异步I/O</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之同步原语/">asyncio之同步原语</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之用控制结构组建协程/">asyncio之用控制结构组建协程</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之并发执行Tasks/">asyncio之并发执行Tasks</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之异步生产结果/">asyncio之异步生产结果</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之定时调度常规函数/">asyncio之定时调度常规函数</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之使用协程处理多任务/">asyncio之使用协程处理多任务</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之异步并发的概念/">asyncio之异步并发的概念</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/Python上下文管理器/">Python上下文管理器</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/Python-importlib讲解/">Python importlib讲解</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/第七章-用Cython包装C库/">第七章 用Cython包装C库</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/08/16/第六章-组织Cython代码/">第六章 组织Cython代码</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/08/10/第五章-Cython和扩展类型/">第五章 Cython和扩展类型</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/31/第四章-Cython实践：N-body模拟/">第四章 Cython实践：N-body模拟</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/31/Python性能分析与调试/">Python性能分析与调试</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/17/第三章-深入Cython/">第三章 深入Cython</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/03/第二章-编译和运行Cython代码/">第二章 编译和运行Cython代码</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/02/第一章-Cython简介/">第一章 Cython简介</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2017-2018 HatBoy
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>





    <script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        $("a").attr("target", "_blank");
        $("#scroll a, #toc a").removeAttr("target");
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>